package com.example.libary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.libary.common.Result;
import com.example.libary.entity.Book;
import com.example.libary.service.BookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 图书控制器
 */
@Tag(name = "图书管理", description = "图书信息管理相关接口")
@RestController
@RequestMapping("/books")
@RequiredArgsConstructor
public class BookController {

    private final BookService bookService;

    /**
     * 分页查询图书列表
     */
    @Operation(summary = "分页查询图书", description = "查询图书列表")
    @GetMapping("/page")
    public Result<Page<Book>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "图书名称") @RequestParam(required = false) String name,
            @Parameter(description = "作者") @RequestParam(required = false) String author,
            @Parameter(description = "分类ID") @RequestParam(required = false) Long categoryId,
            @Parameter(description = "状态") @RequestParam(required = false) String status) {
        Page<Book> page = new Page<>(current, size);
        Page<Book> bookPage = bookService.page(page, name, author, categoryId, status);
        return Result.success(bookPage);
    }

    /**
     * 获取图书详情
     */
    @Operation(summary = "获取图书详情", description = "根据ID获取图书详情")
    @GetMapping("/{id}")
    public Result<Book> getBook(@Parameter(description = "图书ID") @PathVariable Long id) {
        Book book = bookService.getById(id);
        return Result.success(book);
    }

    /**
     * 根据ISBN查询图书
     */
    @Operation(summary = "根据ISBN查询图书", description = "根据ISBN查询图书")
    @GetMapping("/isbn/{isbn}")
    public Result<Book> getBookByIsbn(@Parameter(description = "ISBN") @PathVariable String isbn) {
        Book book = bookService.getByIsbn(isbn);
        return Result.success(book);
    }

    /**
     * 添加图书
     */
    @Operation(summary = "添加图书", description = "管理员权限")
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping
    public Result<Void> addBook(@Parameter(description = "图书信息") @RequestBody @Valid Book book) {
        boolean success = bookService.save(book);
        return success ? Result.success() : Result.error("添加失败");
    }

    /**
     * 更新图书
     */
    @Operation(summary = "更新图书", description = "管理员权限")
    @PreAuthorize("hasRole('ADMIN')")
    @PutMapping("/{id}")
    public Result<Void> updateBook(
            @Parameter(description = "图书ID") @PathVariable Long id,
            @Parameter(description = "图书信息") @RequestBody @Valid Book book) {
        book.setId(id);
        boolean success = bookService.updateById(book);
        return success ? Result.success() : Result.error("更新失败");
    }

    /**
     * 更新图书状态
     */
    @Operation(summary = "更新图书状态", description = "管理员权限")
    @PreAuthorize("hasRole('ADMIN')")
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(
            @Parameter(description = "图书ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam String status) {
        boolean success = bookService.updateStatus(id, status);
        return success ? Result.success() : Result.error("操作失败");
    }

    /**
     * 删除图书
     */
    @Operation(summary = "删除图书", description = "管理员权限")
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/{id}")
    public Result<Void> deleteBook(@Parameter(description = "图书ID") @PathVariable Long id) {
        boolean success = bookService.removeById(id);
        return success ? Result.success() : Result.error("删除失败");
    }
} 